Title
Getting started with arrays in Julia using images
Following this Computational Thinking Course by MIT and JuliaImages and Tutorialspoint's Julia Tutorial
Julia has been touted as the next best thing in computational programming, potentially replacing fan favourite, Python, as the go to language for data science and machine learning.
https://twitter.com/JuliaLanguage/status/1477307810564169731
I have been looking to learn Julia for some time, and have finally gathered the courage to begin, following the MIT's 2021 Computational Thinking course and Tutorialspoint tutorial. The hope is to use Julia for quantitative economics, agent-based models and related interests (and some data science and machine learning of course!).
Some introductory concepts like installation, environments, packages and variables I have skipped to dive into the language. The Tutorialspoint tutorial gives quite good explanations for these.
Arrays are key to computational work, providing the building blocks for programming work in data science and machine learning. In this piece, I will use images to explore arrays. An array in Julia is a set of items "specified with square brackets" that can contain similar items or different items.
- Creating arrays...
arr = [1,2,3]
arr1 = ["Leonard", tan, RGB(0, 1, 1, 1.245)]
Array[1:10]
arr2 = Array{Int64}(undef, 2, 2) #Create a 2x2 array with undefined values of type Integer64
Array[1:5,5:9]
collect(1:2:20)
arr3 = collect(range(1, 15, 30))
Collect uses (start:step:stop) format whilst range uses (start, stop, length).
[1:10...] #using the ellipses
A matrix can be created by specifying the arrays, separating the rows with a semi-colon (;)
matrix = [[1 2];[3 4]]
Compare with..
arr4 = [[1 2], [3 4]]
tensor = Array{Float32}(undef, 3, 3, 3)
- Manipulating arrays..
These functions change the array in place, nota bene.
push!(arr, 10)
pushfirst!(arr, 27)
splice!(arr, 3, 12)
pop!(arr)
- Arrays with images
using Images #importing a package (use add Package to install the package)
path_to_image = "..\\images\\Brain-Teaser2.png"
img = load(path_to_image)
size(img)
img[100, 200]
RGB(0, 0, 1)
brain = img[100:470, 350:900]
save("..\\images\\brain.png", brain)
The save function worked, but why it threw those errors, I am not sure yet. Errors in Julia I haven't yet grasped. Still yet to see a language with helpful error messages!
Kaleidoscope The last element of an array is "end" compared to -1 in python
[
img img[:, end:-1:1]
img[end:-1:1,:] img[end:-1:1, end:-1:1]
]
size(pex)
imgc = rand(RGB{Base.Float32}, 3, 3)
dump(imgc) #dump gives the internal representation of an object
imgc[2,3]
dump(imgc[2,3])
c = imgc[2,3]; (red(c), green(c), blue(c))
[RGB(x, 1, 1) for x in 0:0.1:1.0]
[
RGB(x, y, 1) for x in 0:0.1:1.0, y in 0:0.1:1.0
]
Other cool things in Julia to do with images. To be explored more in a later post. Mosaicview makes it easy to juxtapose images, or stack them. Rot180 and reverse are self explaining.
pex = load("./images/pexels-photo-2422290.jpeg")
mosaicview(img, pex)
mosaicview(img, pex; nrow=1)
rot180(img)
reverse(pex)
- Synthetic images using random numbers
img_gen = randn(300, 250, 3)
colorview(RGB, reshape(img_gen, 3, 300, 250))
colorview(RGB, reshape(img_gen, (3, 250, 300)))